package bard.partnermatchingsystem.domain.R;

import bard.partnermatchingsystem.Common.ErrorCode;
import lombok.Data;

@Data
public class R<T> {
    private int code;

    private T data;

    private String message;

    private String description;

    public static R<Void> ok() {
        return ok(null);
    }

    public static <T> R<T> ok(T data) {
        return new R<>(0, "OK", data, "");
    }

    public static <T> R<T> error(ErrorCode Error) {
        return new R<>(Error.getCode(), Error.getMessage(), null, Error.getDescription());
    }

    public static <T> R<T> error(int code) {
        return new R<>(code, getmessage(code), null, " ");
    }

    public static R error(int code, String description) {
        return new R<>(code, getmessage(code), null, description);
    }

    public R() {
    }

    public R(int code, String message, T data, String description) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.description = description;
    }




    public boolean success() {
        return code == 200;
    }

    public static String getmessage(int code) {
        for (ErrorCode errorCode : ErrorCode.values()) {
            if (errorCode.getCode() == code) {
                return errorCode.getMessage();
            }
        }
        return ErrorCode.SYSTEM_ERROR.getMessage();
    }
}
